const mongoose = require('mongoose')
const { formatDate } = require('../utils/utils')
const schema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    unique: true,
    index: true, // 创建索引
  },
  icon_name: {
    type: String,
    required: true,
    unique: true,
  },
  //更新日期
  date: {
    type: mongoose.SchemaTypes.Date,
    default: Date.now,
    get(val) {
      return formatDate(new Date(val), "yyyy年MM月dd日 hh:mm:ss");
    },
  },
  //文章 ids
  aids: [
    {
      type: mongoose.SchemaTypes.ObjectId,
      ref: "Article",
      index: true, // 添加索引
    },
  ],
});
// 不展示__v字段
schema.set('toJSON', { getters: true, versionKey: false })

module.exports = mongoose.model('Category', schema)
